<?php


namespace app\home\controller;


use app\BaseController;
use app\Request;
use think\facade\Db;

class Lending extends BaseController
{
    protected $middleware = [\app\middleware\Check2::class];

    public function index()
    {
        return view();
    }

    //    分页展示
    public function article(Request $request){

        //获取每页显示的条数
        $limit= $request->param('limit');
        //获取当前页数
        $page= $request->param('page');
        $list = Db::name('book_lending')
            ->alias('b')
            ->join('book c','b.book_id = c.id')
            ->paginate($limit,false,['page'=>$page])
            ->toArray();
        return json(['code'=>0,'msg'=>'','count'=>$list['total'],'data'=>$list['data']]);

    }

    //    还书
    public  function returnBook(){
        $data = request()->param();
        $book = \app\model\Book::find($data['book_id']);
        $book->count  =  $book['count']+1;
        $book->lend_number    = $book['lend_number']-1;
        $book->save();



        $data1 = [
            'book_id' => $data['book_id'],
            'user_id' =>  session('id'),
            'lending_date'    => $data['lending_date'],
            'return_at'     => $data['return_at'],
            'money_fine' => $data['money_fine']
        ];



        $Db = Db::name('book_lending_history');
        $Db->insert($data1);


        Db::name('book_lending')->where('book_id',$data['book_id'])->delete();




    }
}